<?php

/*
* Make Bierden available for translation.
* Translations can be added to the /languages/ directory.
* If you're building a theme based on Twenty Eleven, use
* a find and replace to change 'twentyeleven' to the name
* of your theme in all the template files.
*/

add_action('after_setup_theme', 'bierden_setup');
function bierden_setup(){
	load_theme_textdomain( 'tsv-bierden', get_template_directory() . '/lang' );
}
//load_theme_textdomain( 'tsv-bierden', get_template_directory() . 'lang' );

// Set content width value based on the theme's design
if ( ! isset( $content_width ) )
	$content_width = 1000;

// Register the Sidebars

function bierden_widgets_init() {
	register_sidebar(array(
		'name' => __('Top', 'tsv-bierden'),
		'id' => 'top',
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h2>',
		'after_title' => '</h2>'
	));
	
	register_sidebar(array(
		'name' => __('Sidebar', 'tsv-bierden'),
		'id' => 'sidebar',
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h2>',
		'after_title' => '</h2>'
	)); 
	
	register_sidebar(array(
		'name' => __('Footer Left', 'tsv-bierden'),
		'id' => 'footer-left',
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h2>',
		'after_title' => '</h2>'
	));
	
	register_sidebar(array(
		'name' => __('Footer middle', 'tsv-bierden'),
		'id' => 'footer-middle',
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h2>',
		'after_title' => '</h2>'
	));
	
	register_sidebar(array(
		'name' => __('Footer Right', 'tsv-bierden'),
		'id' => 'footer-right',
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h2>',
		'after_title' => '</h2>'
	));
}
add_action( 'init', 'bierden_widgets_init' );

// Add Scripts and stylesheets

function bierden_scripts_init() {
	
	// Register jQuery as a dependency of domscript.js
	wp_enqueue_script("jquery");
	wp_enqueue_script( 'domscript', get_template_directory_uri() . '/js/domscript.js', array( 'jquery' ) );
}
add_action( 'wp_enqueue_scripts', 'bierden_scripts_init' );


function bierden_styles_init() {
	// Add main stylesheets
	wp_enqueue_style( 'bierden-style-reset', get_template_directory_uri() . '/css/mini-reset.css' );
	wp_enqueue_style( 'bierden-style', get_stylesheet_uri() );
	wp_enqueue_style( 'bierden-style-print', get_template_directory_uri() . '/css/print.css', array(), false, 'print' );
}
add_action( 'wp_enqueue_scripts', 'bierden_styles_init' );


// Auto-discovery feed in header
add_theme_support('automatic-feed-links');

// Article image support. http://codex.wordpress.org/Post_Thumbnails
add_theme_support('post-thumbnails');

// Custom Background-color 
$args = array(
		'default-color' => '000000'
);
add_theme_support( 'custom-background', $args );

$defaults = array(
		'default-image'          => '',
		'random-default'         => false,
		'width'                  => 250,
		'height'                 => 250,
		'flex-height'            => true,
		'flex-width'             => true,
		'default-text-color'     => '000000',
		'header-text'            => true,
		'uploads'                => true,
		'wp-head-callback'       => 'bierden_header_style',
		'admin-head-callback'    => 'bierden_admin_header_style',
		'admin-preview-callback' => 'bierden_admin_header_image',
);
add_theme_support( 'custom-header', $defaults );


/**
 * Styles the header text displayed on the blog.
 *
 * get_header_textcolor() options: 444 is default, hide text (returns 'blank'), or any hex value.
 *
 * @since Twenty Twelve 1.0
*/
function bierden_header_style() {
	$text_color = get_header_textcolor();

	// If no custom options for text are set, let's bail
	if ( $text_color == get_theme_support( 'custom-header', 'default-text-color' ) )
		return;

	// If we get this far, we have custom styles.
	?>
	<style type="text/css">
	<?php
		// Has the text been hidden?
		if ( ! display_header_text() ) :
	?>
		.site-title,
		.site-description {
			position: absolute !important;
			clip: rect(1px 1px 1px 1px); /* IE7 */
			clip: rect(1px, 1px, 1px, 1px);
		}
	<?php
		// If the user has set a custom color for the text, use that.
		else :
	?>
		.site-title a,
		.site-description {
			color: #<?php echo $text_color; ?> !important;
		}
	<?php endif; ?>
	</style>
	<?php
}

/**
 * Styles the header image displayed on the Appearance > Header admin panel.
 *
 * @since Twenty Twelve 1.0
 */
function bierden_admin_header_style() { ?>
	<style type="text/css">
	.appearance_page_custom-header #headimg {
		border: none;
	}
	#headimg h1,
	#headimg h2 {
		line-height: 1.6;
		margin: 0;
		padding: 0;
	}
	#headimg h1 {
		font-size: 30px;
	}
	#headimg h1 a {
		color: #515151;
		text-decoration: none;
	}
	#headimg h1 a:hover {
		color: #21759b;
	}
	#headimg h2 {
		color: #757575;
		font: normal 13px/1.8 "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", sans-serif;
		margin-bottom: 24px;
	}
	#headimg img {
		max-width: <?php echo get_theme_support( 'custom-header', 'max-width' ); ?>px;
	}
	</style>
<?php
}

/**
 * Outputs markup to be displayed on the Appearance > Header admin panel.
 * This callback overrides the default markup displayed there.
 *
 * @since Twenty Twelve 1.0
 */
function bierden_admin_header_image() { ?>
	<div id="headimg">
		<?php
		if ( ! display_header_text() )
			$style = ' style="display:none;"';
		else
			$style = ' style="color:#' . get_header_textcolor() . ';"';
		?>
		<h1><a id="name"<?php echo $style; ?> onclick="return false;" href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php bloginfo( 'name' ); ?></a></h1>
		<h2 id="desc"<?php echo $style; ?>><?php bloginfo( 'description' ); ?></h2>
		<?php $header_image = get_header_image();
		if ( ! empty( $header_image ) ) : ?>
			<img src="<?php echo esc_url( $header_image ); ?>" class="header-image" width="<?php echo get_custom_header()->width; ?>" height="<?php echo get_custom_header()->height; ?>" alt="" />
		<?php endif; ?>
	</div>
<?php }

// Let users choose the primary menu
register_nav_menu( 'primary', __( 'Primary Menu', 'tsv-bierden' ) );

// If its a multi blog
register_nav_menu( 'secondary', __( 'Secondary Menu', 'tsv-bierden' ) );

// Display the Title
function bierden_wp_title( $title, $sep ) {
	global $paged, $page;

	if ( is_feed() )
		return $title;

	// Add the site name.
	$title .= get_bloginfo( 'name' );

	// Add the site description for the home/front page.
	$site_description = get_bloginfo( 'description', 'display' );
	if ( $site_description && ( is_home() || is_front_page() ) )
		$title = "$title $sep $site_description";

	// Add a page number if necessary.
	if ( $paged >= 2 || $page >= 2 )
		$title = "$title $sep " . sprintf( __( 'Page %s', 'tsv-bierden' ), max( $paged, $page ) );

	return $title;
}
add_filter( 'wp_title', 'bierden_wp_title', 10, 2 );

function bierden_entry_meta() {
	// Translators: used between list items, there is a space after the comma.
	$categories_list = get_the_category_list( __( ', ') );

	// Translators: used between list items, there is a space after the comma.
	$tag_list = get_the_tag_list( '', __( ', ') );

	$date = sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s">%4$s</time></a>',
			esc_url( get_permalink() ),
			esc_attr( get_the_time() ),
			esc_attr( get_the_date( 'c' ) ),
			esc_html( get_the_date() )
	);

	$author = sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s" rel="author">%3$s</a></span>',
			esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
			esc_attr( sprintf( __( 'View all posts by %s', 'tsv-bierden' ), get_the_author() ) ),
			get_the_author()
	);

	// Translators: 1 is category, 2 is tag, 3 is the date and 4 is the author's name.
	if ( $tag_list ) {
		$utility_text = __( 'This entry was posted in %1$s and tagged %2$s on %3$s<span class="by-author"> by %4$s</span>.', 'tsv-bierden' );
	} elseif ( $categories_list ) {
		$utility_text = __( 'This entry was posted in %1$s on %3$s<span class="by-author"> by %4$s</span>.', 'tsv-bierden' );
	} else {
		$utility_text = __( 'This entry was posted on %3$s<span class="by-author"> by %4$s</span>.', 'tsv-bierden' );
	}

	printf(
	$utility_text,
	$categories_list,
	$tag_list,
	$date,
	$author
	);
}

/**
 * Include the Theme Options Function File
 *
 * options.php includes the Theme options and Admin Settings page
 * - Define default Theme Options
 * - Register/Initialize Theme Options
 * - Admin Settings Page
 * - Contextual Help
 */
require( get_template_directory() . '/functions/options.php' );

/*
 * add options hook
 */
 
function bierden_header_social_options_display() {
    $options = get_option( 'bierden_theme_social_options' );
    if (!empty($options)) {
	    foreach ( $options as $name => $url ) {
					if (!empty($url)) {
						echo '<script type="text/javascript" src="' . $url . '"></script>';
					}
	    }
	}
}

add_action('wp_head', 'bierden_header_social_options_display');

/**
 * Include own widget
 */
require( get_template_directory() . '/functions/easy-coach-widget.php' );